package com.lej.core.utils.extention

import android.graphics.Color
import com.lej.core.base.app
import com.lej.core.dsl.dp
import kotlin.math.max
import kotlin.math.min

/**
 * 修改颜色透明度
 * @param color
 * *
 * @param alpha
 * *
 * @return
 */
fun Int.changeAlpha(alpha: Int): Int {
    val red = Color.red(this)
    val green = Color.green(this)
    val blue = Color.blue(this)

    return Color.argb(alpha, red, green, blue)
}

fun Double.dp2px(): Int {
    val scale: Float = app.resources.displayMetrics.density
    return (this * scale + 0.5f).toInt()
}

fun Int.dp2Px() = this.toDouble().dp2px()
//val Int.dpi get()= this.toDouble().dp2px()
val Int.dpf get()= this.dp.toFloat()
fun Int?.safe(default: Int = 0) = if (this == null || this == 0) default else this
fun Float?.safe(default: Float = 0f) = if (this == null || this == 0f) default else this

/**约束取值范围,超出则取范围边界值*/
fun Int.keepScope(min: Int, max: Int) = min(max(this, min), max)

fun Float.keepScope(min: Float, max: Float) = min(max(this, min), max)

fun Long.keepScope(min: Long, max: Long) = min(max(this, min), max)